﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;
using System.IO;
using NPOI.HSSF.UserModel;

namespace Zwms.Client.InfoWms
{
    public partial class LocationWnd : UserControl
    {
        #region 成员函数
        private WhLocation _currentQueryRecord;
        private PagerIndex _pagerIndex;
        private WhLocation _selectedRecord;
        public WhLocation SelectedRecord { get { return _selectedRecord; } }
        private CommonUI.WaitForm _waitForm = new CommonUI.WaitForm();
        #endregion

        public LocationWnd()
        {
            InitializeComponent();
            InitBtnImage();
            InitControl();
        }

        public LocationWnd(WhLocation record)
        {
            InitializeComponent();
            InitBtnImage();
            this._currentQueryRecord = record;
            ResetDataPager();
            RefreshData();
        }


        #region 公共函数
        public void SetQueryState()
        {
            btnNew.Enabled = false;
            btnEdit.Enabled = false;
            btnDelete.Enabled = false;

            btnConfirm.Visible = true;
            btnCancel.Visible = true;
        }

        public void SetParaQueryState()
        {
            btnNew.Visible = false;
            btnEdit.Visible = false;
            btnDelete.Visible = false;
            btnQuery.Visible = false;

            btnParaQuery.Visible = true;
            btnConfirm.Visible = true;
            btnCancel.Visible = true;
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnNew.Image = global::Zwms.Client.Properties.Resources.add;
            this.btnEdit.Image = global::Zwms.Client.Properties.Resources.edit;
            this.btnDelete.Image = global::Zwms.Client.Properties.Resources.remove;
            this.btnInput.Image = global::Zwms.Client.Properties.Resources.list;
            this.btnQuery.Image = global::Zwms.Client.Properties.Resources.query;
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.exit;
            this.btnParaQuery.Image = global::Zwms.Client.Properties.Resources.query;
        }
        private void InitControl()
        {
            ResetDataPager();
            ResetQueryRecord();
            RefreshData();
        }

        private void ResetDataPager()
        {
            dataPager.PageCurrent = 1;
            dataPager.PageSize = CommonDefine.iPageSize;
        }

        private void ResetQueryRecord()
        {
            _currentQueryRecord = new WhLocation();
        }

        private void RefreshData()
        {
            //读入部分数据
            _pagerIndex = new PagerIndex(dataPager.PageCurrent, CommonDefine.iPageSize);
            MyDataBind(_currentQueryRecord, _pagerIndex);
        }

        private void Add()
        {
           LocationForm window = new LocationForm(EnumOpenType.New, null,null);
            if (window.ShowDialog() == DialogResult.OK)
            {
                RefreshData();
            }
        }
        private void Edit()
        {
            if (dgv.CurrentCell == null)
            {
                MessageBox.Show("请选择要编辑的项");
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            WhLocation record = row.DataBoundItem as WhLocation;
            LocationForm window = new LocationForm(EnumOpenType.Open,record.LocationCode,record.WarehouseCode);
            if (window.ShowDialog() == DialogResult.OK)
            {
                RefreshData();
            }
        }
        private void Delete()
        {
            if (dgv.CurrentCell == null)
            {
                MessageBox.Show("请选择要删除的项");
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;
            if (rowIndex < 0)
            {
                return;
            }

            if (MessageBox.Show("是否确认删除所选择的项？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }

            DataGridViewRow row = dgv.Rows[rowIndex];
            WhLocation record = row.DataBoundItem as WhLocation;
            try
            {
                //删除操作
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                client.DeleteLocation(record);

                //删除列表中的该行记录
                InitControl();
            }
            catch (System.Exception exc)
            {
                MessageBox.Show(this, "删除项出错，错误：" + exc.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void MyDataBind(WhLocation queryRecord, PagerIndex pIndex)
        {
            try
            {
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                IList<WhLocation> queryList = client.QueryLocationList(queryRecord, ref pIndex);
                dataPager.RecordCount = pIndex.RecordCount;
                LoadBillDetail(queryList);
            }
            catch
            {
            }
        }

        private void LoadBillDetail(IList<WhLocation> recordList)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                this.whLocationBindingSource.DataSource = null;
                this.whLocationBindingSource.DataSource = recordList;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void SelectConfirm()
        {
            if (dgv.CurrentCell == null)
            {
                MessageBox.Show("请首先在列表中选中项！");
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                MessageBox.Show("请首先在列表中选中项！");
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            _selectedRecord = row.DataBoundItem as WhLocation;
            if (this.Parent is Form)
            {
                Form window = this.Parent as Form;
                window.DialogResult = DialogResult.OK;
                window.Close();
            }
            else
            {
                return;
            }
        }
        #endregion

        #region 控件函数
        private void btnNew_Click(object sender, EventArgs e)
        {
            Add();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            ResetDataPager();
            ResetQueryRecord();
            if (tbLocationCode.Text != "")
            {
                _currentQueryRecord.Id = tbLocationCode.Text;
            }
            if (tbGoodsCode.Text != "")
            {
                _currentQueryRecord.StoreGoodsCode = tbGoodsCode.Text;
            }
            RefreshData();
        }

        private void dataPager_BtnClick(object sender, EventArgs e)
        {
            _pagerIndex.CurrentPageIndex = dataPager.PageCurrent;
            MyDataBind(_currentQueryRecord, _pagerIndex);
        }

        private void dgv_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            SelectConfirm();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            SelectConfirm();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (this.Parent is Form)
            {
                Form window = this.Parent as Form;
                window.DialogResult = DialogResult.Cancel;
                window.Close();
            }
            else
            {
                return;
            }
        }

        private void btnParaQuery_Click(object sender, EventArgs e)
        {
            ResetDataPager();
            _currentQueryRecord.Id = tbLocationCode.Text;
            _currentQueryRecord.LocationName = tbGoodsCode.Text;
            RefreshData();
        }

        private void btnInput_Click(object sender, EventArgs e)
        {
            //从Excel导入入库计划单
            //1.首先读入列表
            OpenFileDialog oDlg = new OpenFileDialog();
            oDlg.Filter = "*.xls|*.xls";
            if (oDlg.ShowDialog() == DialogResult.OK)
            {
                //打开文件
                FileStream fileTemplate = new FileStream(oDlg.FileName, FileMode.Open, FileAccess.Read);
                HSSFWorkbook hssfworkbook = new HSSFWorkbook(fileTemplate);
                //获取工作簿
                HSSFSheet sheet = hssfworkbook.GetSheetAt(0) as HSSFSheet;
                int iStartRow = 1;
                HSSFRow row = sheet.GetRow(iStartRow) as HSSFRow;
                ServiceReferenceZwms.ServiceZwmsClient client
                    = new ServiceReferenceZwms.ServiceZwmsClient();
                while (row != null && row.GetCell(0) != null && row.GetCell(0).ToString() != "")
                {
                    string sWarehouseCode = "";
                    string sShelfCode = "";
                    string sLocationCode = "";
                    string sGoodsCode = "";

                    if (row.GetCell(0) != null)
                    {
                        try
                        {
                            sWarehouseCode = row.GetCell(0).ToString();
                        }
                        catch
                        {
                            sWarehouseCode = "";
                        }
                    }
                    if (row.GetCell(2) != null)
                    {
                        try
                        {
                            sShelfCode = row.GetCell(2).ToString();
                        }
                        catch
                        {
                            sShelfCode = "";
                        }
                    }
                    if (row.GetCell(3) != null)
                    {
                        try
                        {
                            sLocationCode = row.GetCell(3).ToString();
                        }
                        catch
                        {
                            sLocationCode = "";
                        }
                    }
                    if (row.GetCell(4) != null)
                    {
                        try
                        {
                            sGoodsCode = row.GetCell(4).ToString();
                        }
                        catch
                        {
                            sGoodsCode = "";
                        }
                    }

                    WhLocation location = new WhLocation();
                    location.WarehouseCode = sWarehouseCode;
                    location.WarehouseName = sWarehouseCode;
                    location.ShelfCode = sShelfCode;
                    location.ShelfName = sShelfCode;
                    location.LocationCode = sLocationCode;
                    location.LocationName = sLocationCode;
                    location.StoreGoodsCode = sGoodsCode;
                    client.AddLocation(location);
                    iStartRow++;
                    row = sheet.GetRow(iStartRow) as HSSFRow;
                }
                MessageBox.Show("导入完毕！");
            }
        }
        #endregion
    }
}
